package com.dz.controller.manager;

import com.dz.annotation.OperationLog;
import com.dz.bean.Visitor;
import com.dz.service.VisitorService;
import com.dz.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@CrossOrigin//允许跨域访问
@RequestMapping("/m/visitor")
public class VisitorManagerController {

    @Autowired
    VisitorService visitorService;

    @GetMapping("page")
    public Result page(Page page, Visitor visitor){
        visitorService.page(page,visitor);
        return Result.success(page);
    }

    @PostMapping
    @OperationLog("添加游客")
    public Result add(@RequestBody Visitor visitor){
        // 设置默认密码并加密
        String defaultPassword = "123456"; // 默认密码
        visitor.setPassword(PasswordUtil.getEncryption(defaultPassword));

        Object obj = visitorService.add(visitor);
        return Result.success(obj);
    }

    @DeleteMapping("/{id}")
    @OperationLog("删除游客")
    public Result delete(@PathVariable("id") Integer id){
        Object obj = visitorService.delete(id);
        return Result.success(obj);
    }

    @PutMapping
    @OperationLog("修改游客信息")
    public Result edit(@RequestBody Visitor visitor){
        Object obj = visitorService.edit(visitor);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all(Visitor visitor){
        Object obj = visitorService.all(visitor);
        return Result.success(obj);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Object obj = visitorService.findById(id);
        return Result.success(obj);
    }
}
